/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ParseDICOM_Test;

/**
 *
 * @author hshin
 */

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;



// For write operation
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;


public class TransformDICOMparseResult {
        public void writeToXML(String directoryname, String sourcefilename){

            ReadDICOMparseResult saxReader = new ReadDICOMparseResult();

            try{
                String XMLFileName = "parseDICOMresult.xml";
                String XMLFileNameAbs = directoryname + "/" + XMLFileName;

                File f = new File(directoryname + "/" + sourcefilename);

                // Use a Transformer for output
                TransformerFactory tFactory = TransformerFactory.newInstance();
                Transformer transformer = tFactory.newTransformer();

                // Use the parser as a SAX source for input
                FileReader fr = new FileReader(f);
                BufferedReader br = new BufferedReader(fr);
                InputSource inputSource = new InputSource(fr);                
                SAXSource source = new SAXSource(saxReader, inputSource);
                StreamResult result = new StreamResult(XMLFileNameAbs);
                transformer.transform(source, result);

            }catch(TransformerConfigurationException tce){
                // Error generated by the parser
                System.out.println("\n** Transformer Factory error");
                System.out.println("    " + tce.getMessage());

                // Use the contained exception, if any
                Throwable x = tce;

                if(tce.getException() != null){
                    x = tce.getException();
                }

                x.printStackTrace();
            }catch(TransformerException te){
                // Error generated by the parser
                System.out.println("\n** Transformation error");
                System.out.println("    " + te.getMessage());

                // Use the contained exception, if any
                Throwable x = te;

                if(te.getException() != null){
                    x = te.getException();
                }

                x.printStackTrace();
            }catch(IOException ioe){
                // I/O Error
                ioe.printStackTrace();
            }
    }
}
